package com.tinglv.spring.common;

import java.io.File;

import it.sauronsoftware.jave.*;

/**
 * 音频、视频相互转换格式工具类
 *
 * @author <a href="mailto:hecc@ifengmao.com">hecc</a>
 * @version 1.0
 */
public class JaveUtils {

    private static final String AUDIO_WAV = "wav";
    private static final String AUDIO_MP3 = "mp3";
    private static final String VIDEO_MP4 = "mpegvideo";
    private static final String VIDEO_3GP = "3gp";
    private static final String VIDEO_FLV = "flv";
    private static final String VIDEO_AVI = "avi";

    /**
     * 转换为mp3音频
     *
     * @param source
     * @param targetFile
     * @return
     * @throws Exception
     */
    public static File radioMp3(File source, String targetFile) throws Exception {
        return radio(source, targetFile, AUDIO_MP3);
    }

    /**
     * 转换为指定格式音频
     *
     * @param source
     * @param targetFile
     * @param format
     * @return
     * @throws Exception
     */
    public static File radio(File source, String targetFile, String format) throws Exception {
        File target = new File(targetFile);
        AudioAttributes audio = new AudioAttributes();
        EncodingAttributes attrs = new EncodingAttributes();
        if (AUDIO_MP3.equals(format)) {
            audio.setCodec("libmp3lame");
            //音频比率 MP3默认是1280000
            audio.setBitRate(new Integer(36000));
            audio.setChannels(new Integer(2));
            audio.setSamplingRate(new Integer(44100));
            attrs.setFormat("mp3");
        } else if (AUDIO_WAV.equals(format)) {
            // 默认格式wav
            audio.setCodec("pcm_s16le");
            attrs.setFormat("wav");
        }
        attrs.setAudioAttributes(audio);
        Encoder encoder = new Encoder();
        encoder.encode(source, target, attrs);
        return execute(source, targetFile, audio, attrs, null);
    }

    /**
     * 转换为3gp视频
     *
     * @param source
     * @param targetFile
     * @return
     * @throws Exception
     */
    public static File videoMp4(File source, String targetFile) throws Exception {
        return video(source, targetFile, VIDEO_MP4);
    }

    /**
     * 转换视频格式
     *
     * @param source
     * @param targetFile
     * @param format
     * @return
     * @throws Exception
     */
    public static File video(File source, String targetFile, String format) throws Exception {
        AudioAttributes audio = new AudioAttributes();
        EncodingAttributes attrs = new EncodingAttributes();
        VideoAttributes video = new VideoAttributes();
        if (VIDEO_FLV.equals(format)) {
            audio.setCodec("libmp3lame");
            audio.setBitRate(new Integer(64000));
            audio.setChannels(new Integer(1));
            audio.setSamplingRate(new Integer(22050));
            video.setCodec("flv");
            video.setBitRate(new Integer(160000));
            video.setFrameRate(new Integer(15));
            video.setSize(new VideoSize(400, 300));
            attrs.setFormat("flv");
        } else if (VIDEO_AVI.equals(format)) {
            audio.setCodec("libmp3lame");
            audio.setBitRate(new Integer(56000));
            audio.setChannels(new Integer(1));
            audio.setSamplingRate(new Integer(22050));
            video.setCodec(VideoAttributes.DIRECT_STREAM_COPY);
            attrs.setFormat("avi");
        } else if (VIDEO_MP4.equals(format)) {
            audio.setCodec("libvorbis");
            video.setCodec("mpeg4");
            video.setTag("DIVX");
            video.setBitRate(new Integer(160000));
            video.setFrameRate(new Integer(30));
            attrs.setFormat("mpegvideo");
        } else if (VIDEO_3GP.equals(format)) {
            audio.setCodec("libfaac");
            audio.setBitRate(new Integer(128000));
            audio.setSamplingRate(new Integer(44100));
            audio.setChannels(new Integer(2));
            video.setCodec("mpeg4");
            video.setBitRate(new Integer(160000));
            video.setFrameRate(new Integer(15));
            video.setSize(new VideoSize(176, 144));
            attrs.setFormat("3gp");
        }
        return execute(source, targetFile, audio, attrs, video);
    }

    /**
     * 执行转化过程
     *
     * @param source     源文件
     * @param targetFile 目标文件
     * @param audio      音频
     * @param attrs      编码
     * @param video      视频
     * @return
     * @throws Exception
     */
    public static File execute(File source, String targetFile, AudioAttributes audio, EncodingAttributes attrs,
            VideoAttributes video) throws Exception {
        if (source.exists()) {
            File target = new File(targetFile);
            attrs.setAudioAttributes(audio);
            if (video != null) {
                attrs.setVideoAttributes(video);
            }
            Encoder encoder = new Encoder();
            encoder.encode(source, target, attrs);
            return target;
        }
        return null;
    }

    /**
     * 主函数测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        /*File file = new File("C://Users//admin//Music//music//wx_yao_ing.wav");
        radioMp3(file, "C://Users//admin//Music//music//111wav-mp3.mp3");
        File file2 = new File("C://Users//admin//Music//music//111wav-mp3.mp3");
        radio(file2, "C://Users//admin//Music//music//111mp3-WAV.wav", AUDIO_WAV);*/

        File file3 = new File("C://Users//admin//Music//music//mm-avi.avi");
        video(file3, "C://Users//admin//Music//music//mm-flv.flv", VIDEO_FLV);
    }

}
